package com.slaffka.report;

import com.slaffka.action.ReportAction;
import com.slaffka.dao.UserDAO;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.base.JRBaseField;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JRDesignField;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;

import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: slaffka
 * Date: 18.11.12
 * Time: 22:22
 * To change this template use File | Settings | File Templates.
 */

@WebServlet(urlPatterns = "/export")
public class ExportServlet extends HttpServlet {
    JasperPrint jasperPrint;
    ServletOutputStream servletOutputStream;
    private List allUsers;

    @Inject
    UserDAO userDAO;

    @Inject
    ReportAction reportAction;

//    public void initReport() throws JRException {
//        allUsers = userDAO.getAllUsers();
//        JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(allUsers);
//
//        String path = getServletContext().getRealPath("\\WEB-INF\\jasperreports\\report.jasper");
//        jasperPrint = JasperFillManager.fillReport(path, new HashMap(), beanCollectionDataSource);
//    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            jasperPrint = reportAction.getCurrentJasperPrint();
            String documentTypeStr = request.getParameter("documentType");
            servletOutputStream = response.getOutputStream();
            //request.getServletContext().getRealPath("/WEB-INF/jasperreports/")
            DocumentType documentType = DocumentType.getDocumentTypeByName(documentTypeStr);
            exportToFile(response, documentType);
        } catch (JRException e){
            e.printStackTrace();
        }
    }

    private void exportToFile(HttpServletResponse response, DocumentType documentType) throws JRException {
        if (documentType == null){
            return;
        }

        response.setContentType("application/" + documentType.name());
        response.addHeader("Content-disposition", "attachment; filename=report." + documentType.name());

        if (DocumentType.PDF.equals(documentType)){
            JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
        }

        if (DocumentType.XLSX.equals(documentType)){
            exportToMicrosoftOfficeFile(new JRXlsxExporter());
        }

        if (DocumentType.DOCX.equals(documentType)){
            exportToMicrosoftOfficeFile(new JRDocxExporter());
        }
    }

    private void exportToMicrosoftOfficeFile(JRAbstractExporter docxExporter) throws JRException{
        docxExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        docxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream);
        docxExporter.exportReport();
    }
}
